package org.xshop.web.biz;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xshop.model.TGoodsBrand;
import org.xshop.service.ITGoodsBrandService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import top.ibase4j.core.base.BaseController;
import top.ibase4j.core.util.UploadUtil;
import top.ibase4j.core.util.WebUtil;

/**
 * <p>
 * 品牌  前端控制器
 * </p>
 *
 * @author ShenHuaJie123
 * @since 2018-03-01
 */
@RestController
@RequestMapping("/goodsBrand")
@Api(value = "品牌接口", description = "品牌接口")
public class TGoodsBrandController extends BaseController<TGoodsBrand, ITGoodsBrandService> {
    @RequiresPermissions("goodsBrand.read")
    @PutMapping(value = "/read/list")
    @ApiOperation(value = "查询品牌", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object query(HttpServletRequest request) {
        Map<String, Object> param = WebUtil.getParameter(request);
        return super.query(param);
    }

    @RequiresPermissions("goodsBrand.read")
    @PutMapping(value = "/read/detail")
    @ApiOperation(value = "品牌详情", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object get(HttpServletRequest request) {
        TGoodsBrand param = WebUtil.getParameter(request, TGoodsBrand.class);
        return super.get(param);
    }

    @PostMapping
    @RequiresPermissions("goodsBrand.update")
    @ApiOperation(value = "修改品牌", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object update(HttpServletRequest request) {
        TGoodsBrand param = WebUtil.getParameter(request, TGoodsBrand.class);
        String filePath = UploadUtil.getUploadDir(request);
        String name = param.getBrandLogo();
        if (StringUtils.isNotBlank(name) && !name.toLowerCase().contains("http")) {
            String path = UploadUtil.remove2FDFS(filePath + name).getRemotePath();
            param.setBrandLogo(path);
        }
        return super.update(param);
    }

    @DeleteMapping
    @RequiresPermissions("goodsBrand.delete")
    @ApiOperation(value = "删除品牌", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object del(HttpServletRequest request) {
        TGoodsBrand param = WebUtil.getParameter(request, TGoodsBrand.class);
        Assert.notNull(param.getId(), "ID不能为空");
        return super.delete(param);
    }
}